package com.project.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.bean.RoomBean;
import com.project.bean.StudentBean;
import com.project.mapper.IRoomMapper;
import com.project.service.IRoomService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class RoomServiceImpl extends BaseService implements IRoomService {
    @Override
    public PageInfo<RoomBean> cutByItem(Integer pageNO, String address, String roomState, String roomCanNum, String roomGender) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        PageHelper.startPage(pageNO, 2);
        PageInfo<RoomBean> pg = PageInfo.of(mapper.cutByItem(address, roomState, roomCanNum, roomGender));
        session.close();
        return pg;
    }

    @Override
    public List<RoomBean> roomByGender(String stuGender) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        List<RoomBean> list = mapper.roomByGender(stuGender);
        session.close();
        return list;
    }

    @Override
    public void add(RoomBean room) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        mapper.add(room);
        session.commit();
        session.close();
    }

    @Override
    public boolean del(Integer roomId) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        Integer roomNum = mapper.findByRoomId(roomId);
        if (roomNum == 0) {
            mapper.del(roomId);
            session.commit();
            session.close();
            return true;
        } else {
            return false;
        }
    }

    @Override
    public RoomBean findById(Integer roomId) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        RoomBean roomBean = mapper.findById(roomId);
        session.close();
        return roomBean;
    }
}
